package buaa.com.crazzyguess;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import buaa.com.adapter.GameAdapter;
import buaa.com.util.CrazyDao;
import buaa.com.util.ImageUtil;

public class MainActivity extends AppCompatActivity {
    private GridView grid;
    private LinearLayout topBar;
    private TextView topStar;
    private List<Map<String,Object>> values;
    private GameAdapter gameAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        grid = (GridView)findViewById(R.id.grid);
        topBar =(LinearLayout)findViewById(R.id.top_bar);
        topStar = (TextView)findViewById(R.id.top_star);

        topBar.setBackgroundDrawable(new BitmapDrawable(this.getResources(), ImageUtil.getTopBar()));
        topStar.setBackgroundDrawable(new BitmapDrawable(this.getResources(), ImageUtil.getTopStar()));

        int nowStage = getSharedPreferences("game",MODE_PRIVATE).getInt("nowStage",0);
        Log.d("nowStage-values...",nowStage+"");
        topStar.setText(String.valueOf(nowStage));
        values = CrazyDao.getStageData(nowStage);
        gameAdapter = new GameAdapter(values,this);
        grid.setAdapter(gameAdapter);

        grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Map<String,Object> map = values.get(position);
                if((boolean)map.get("overFlag")){
                    Intent intent = new Intent(MainActivity.this,GameActivity.class);
                    intent.putExtra("id",(int)map.get("stage"));
                    startActivity(intent);
                }else{
                    Toast.makeText(MainActivity.this,"不能玩",Toast.LENGTH_LONG).show();
                }
            }
        });

    }
}
